//
//  ParameterTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/17.
//

import UIKit

class ParameterTableViewCell: UITableViewCell {
    var bgView: UIView!
    var collectionView: UICollectionView!
    var contentModel: CarDetailsContentModel!
    var bgLayer1:CALayer!
    var seaCarFlag: Bool = false
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = false
        
        bgLayer1 = CALayer()
        bgLayer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        self.bgView.layer.addSublayer(bgLayer1)
        // shadowCode
        self.bgView.layer.shadowColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 0.9).cgColor
        self.bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.bgView.layer.shadowOpacity = 1
        self.bgView.layer.shadowRadius = 10
        self.contentView.addSubview(bgView)
        
        
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (kScreenWidth - 28)/3, height: 84)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        
        self.collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        self.collectionView.register(ParameterCollectionViewCell.self, forCellWithReuseIdentifier: "ParameterCollectionViewCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.isScrollEnabled = false
        self.bgView.addSubview(self.collectionView)
        
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(168)
            make.bottom.equalTo(-10)
        }
        
        bgLayer1.frame = bgView.bounds
        
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(168).priority(UILayoutPriority.defaultLow)
            make.bottom.equalTo(0)
        }
        
        
    }
    
    
    
    //model
    func setContentModel(contentModel: CarDetailsContentModel){
        self.contentModel = contentModel
        self.collectionView.reloadData()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}


extension ParameterTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ParameterCollectionViewCell", for: indexPath) as! ParameterCollectionViewCell
        
        switch indexPath.row {
            case 0:
                cell.imageView.image = UIImage(named: "icon_car_year")
                cell.titleLab.text = MMLocalizedString("particular_year", comment: "年份")
                cell.contentLab.text = self.contentModel?.exfactoryDate ?? ""
            case 1:
                cell.imageView.image = UIImage(named: "icon_car_mileage")
                cell.titleLab.text = MMLocalizedString("mileage", comment: "里数")
                
                let tempMileageUnit = XTManageCenter.mileageUnit() //公里
                let mileage = XTManageCenter.showPriceInComma(source: (self.contentModel?.mileage ?? 0).cleanZero)
                cell.contentLab.text = String(format: "%@%@",
                                              mileage,
                                              tempMileageUnit)
                
            case 2:
                cell.imageView.image = UIImage(named: "icon_car_derailleur")
                cell.titleLab.text = MMLocalizedString("derailleur", comment: "变速器")
                cell.contentLab.text = self.contentModel?.gearbox ?? ""
            case 3:
                if self.seaCarFlag {
                    cell.imageView.image = UIImage(named: "icon_car_charact")
                    cell.titleLab.text = MMLocalizedString("exterior_color", comment: "车身颜色")
                    cell.contentLab.text = self.contentModel?.colorname ?? ""
                }else{
                    cell.imageView.image = UIImage(named: "icon_car_charact")
                    cell.titleLab.text = MMLocalizedString("characteristic", comment: "首数")
                    cell.contentLab.text = self.contentModel?.ownerNum ?? ""
                }
            case 4:
                cell.imageView.image = UIImage(named: "icon_car_volume")
                cell.titleLab.text = MMLocalizedString("volume", comment: "容积")
                let engine = XTManageCenter.showPriceInComma(source: self.contentModel?.engine ?? "")
                cell.contentLab.text = engine
            default:
                
                cell.imageView.image = UIImage(named: "icon_car_aiming")
                cell.titleLab.text = MMLocalizedString("aiming_circle", comment: "方向盘")
                if self.contentModel?.steeringWheel  == 1{
                    cell.contentLab.text = MMLocalizedString("offside", comment: "右侧")
                }else{
                    cell.contentLab.text = MMLocalizedString("left_side", comment: "左侧")
                }
                
        }
        return cell
    }
    
}

